/**
 * @FileName: BaseH5Controller.java
 * @Author
 * @Description:H5页面接口基础类
 * @Date 2016年3月15日 上午9:57:16
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.h5;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;

import cn.hpclub.server.interceptor.H5ApiIntercepter;
import com.jfinal.aop.Before;
import org.json.JSONObject;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

import cn.hpclub.server.constant.H5ApiConstant;
import cn.hpclub.server.service.MemberOrgService;
@Before(H5ApiIntercepter.class)
public class BaseH5Controller extends Controller{

    /**
     * @Title: setResponseObj
     * @Description:封装返回的数据结构
     * @param code
     * @param msg
     * @param data
     * @return JSONObject
     */
    public JSONObject setResponseObj(int code, String msg, Object data){
        JSONObject rjson = new JSONObject();

        rjson.put(H5ApiConstant.Key.RESULT_CODE, code);
        rjson.put(H5ApiConstant.Key.MESSAGE, msg);

        if(data != null){
            rjson.put(H5ApiConstant.Key.DATA, data);
        }

        return rjson;
    }

    public String getHttpRequestContent(HttpServletRequest request) throws IOException{
        String inputStr = null;
        InputStream inputStream = null;
        if(request != null){
            try{
                inputStream = request.getInputStream();
                inputStr = stream2String(inputStream, "UTF-8");
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                if(inputStream != null){
                    try{
                        inputStream.close();
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
        return inputStr;
    }

    private String stream2String(InputStream inputStream, String encoding) throws IOException{
        InputStreamReader inputStreamReader = null;
        StringWriter stringWriter = new StringWriter();
        String result = null;
        try{
            if(encoding == null || "".equals(encoding)){
                inputStreamReader = new InputStreamReader(inputStream);
            } else{
                inputStreamReader = new InputStreamReader(inputStream, encoding);
            }

            // 将输入流写入输出流
            char[] buffer = new char[1024];
            int n = 0;
            while(-1 != (n = inputStreamReader.read(buffer))){
                stringWriter.write(buffer, 0, n);
            }
            if(stringWriter != null){
                result = stringWriter.toString();
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(inputStreamReader != null){
                inputStreamReader.close();
            }
            if(stringWriter != null){
                stringWriter.close();
            }
        }
        return result;
    }

    /**
     * 验证用户合法性，避免只传userId形成安全漏洞
     * 
     * @Title: isUserValid
     * @Description:TODO
     * @param userId
     * @param token
     * @return boolean
     */
    public boolean isUserValid(String userId, String token){
        boolean isValid = false;
        if(StrKit.notBlank(token) && token.length() >= 11){
            String phone = token.substring(token.length() - 11);
            String userName = MemberOrgService.service.getUserNameByUserId(userId);
            if(phone.equals(userName)){
                isValid = true;
            }
        }

        return isValid;
    }

    /**
     * @Title: validRequestParams
     * @Description:验证必传入参的公共方法
     * @return String
     */
    public String validRequestParams(String userId, String token){
        JSONObject jo = null;
        if(StrKit.isBlank(userId) || StrKit.isBlank(token)){
            jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.PARA_ILLEGAL,
                                H5ApiConstant.Msg.EMPTY);
            return jo.toString();
        }
        // 验证用户有效性
        if(!isUserValid(userId, token)){
            jo = setResponseObj(H5ApiConstant.ResultCode.FAIL_CODE, H5ApiConstant.Msg.ILLEGAL_USER,
                                H5ApiConstant.Msg.EMPTY);
            return jo.toString();
        }

        return "";
    }
}
